package com.cby.project.mvi.flow

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.cby.android.olive.ext.showToast
import com.cby.project.mvi.R
import com.cby.project.mvi.ext.observeEvent
import com.cby.project.mvi.state.NetworkViewAction
import com.cby.project.mvi.state.NetworkViewEvent
import com.cby.project.mvi.state.PageStatus
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * @description: FlowActivity
 *
 * @author ChenBaoyang
 * @date 2021/12/4 10:37
 */
class FlowActivity : AppCompatActivity() {

    private val mViewModel by viewModel<FlowViewModel>()

    private lateinit var pageLoadBtn: Button
    private lateinit var partLoadBtn: Button
    private lateinit var multiLoadBtn: Button
    private lateinit var errorLoadBtn: Button
    private lateinit var resultTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flow)
        pageLoadBtn = findViewById(R.id.btn_page_load)
        partLoadBtn = findViewById(R.id.btn_part_load)
        multiLoadBtn = findViewById(R.id.btn_multi_load)
        errorLoadBtn = findViewById(R.id.btn_error_load)
        resultTv = findViewById(R.id.tv_result)


        mViewModel.viewEvents.observeEvent(this){
            when(it) {
                is NetworkViewEvent.DismissLoadingDialog -> resultTv.text = "取消加载弹窗"
                is NetworkViewEvent.ShowLoadingDialog -> resultTv.text = "显示加载弹窗"
                is NetworkViewEvent.ShowToast -> showToast("点赞成功")
            }
            //Snackbar.make(resultTv, , Snackbar.LENGTH_SHORT).show()
        }

        mViewModel.viewStates.observe(this) {
            when(it.pageStatus) {
                is PageStatus.Loading -> resultTv.text = "取消加载弹窗"
                is PageStatus.Success -> resultTv.text = "页面请求成功"
                is PageStatus.Error -> showToast(it.pageStatus.throwable.toString())
            }
        }

        pageLoadBtn.setOnClickListener {
            mViewModel.dispatch(NetworkViewAction.PageRequest)
        }

        partLoadBtn.setOnClickListener {
            mViewModel.dispatch(NetworkViewAction.PartRequest)
        }

        multiLoadBtn.setOnClickListener {
            mViewModel.dispatch(NetworkViewAction.MultiRequest)
        }

        errorLoadBtn.setOnClickListener {
            mViewModel.dispatch(NetworkViewAction.ErrorRequest)
        }


    }
}